package com.zed.code.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zed.code.base.TRequest;
import com.zed.code.base.TResponse;
import com.zed.code.request.*;
import com.zed.code.response.*;
import com.zed.code.service.PushService;
import com.zed.code.service.QueryService;
import com.zed.code.util.GDEVConfig;
import com.zed.code.util.PubFetchTool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/V1")
public class V1QueryController {

    private Logger log = Logger.getLogger(V1QueryController.class);
    @Autowired
    private QueryService queryService;
    @Autowired
    private PushService pushService;

    @RequestMapping(value = "/query_token", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public TResponse<TokenInfo> queryToken(@RequestBody TRequest<TokenReq> tRequest) throws IOException {
        if (tRequest == null || tRequest.getData() == null) {
            throw new IllegalArgumentException("illegal argument exception, has an illegal third com.zed.code.request");
        }
        TResponse<TokenInfo> response=queryService.query_token(tRequest);
        return response;
    }
    /**
     * 查询充电桩信息
     * */
    @RequestMapping(value ="/query_stations_info",method = {RequestMethod.POST})
    @ResponseBody
    public TResponse<StationInfoPage> query_stations_info(@RequestBody TRequest<StationsInfo> tRequest) throws IOException {
        if (null==tRequest){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        StationsInfo stationsInfo= tRequest.getData();
        if (null==stationsInfo){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        stationsInfo.setPageNo(stationsInfo.getPageNo()==null?1:stationsInfo.getPageNo());
        stationsInfo.setPageSize(stationsInfo.getPageSize()==null?10:stationsInfo.getPageSize());
        TResponse<StationInfoPage> tokenInfo=queryService.queryStationsInfo(stationsInfo,GDEVConfig.OPERATOR_ID);
        return tokenInfo;

    }

    //设备接口状态查询
    @RequestMapping(value ="/query_station_status",method = {RequestMethod.POST})
    @ResponseBody
    public TResponse queryStationStatus(@RequestBody TResponse<StationIDs> tRequest) throws IOException {
        if (null==tRequest){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        StationIDs stationIDs= tRequest.getData();
        if (stationIDs==null||stationIDs.getStationIDs().size()==0){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        TResponse resMap=queryService.queryStationStatus(stationIDs.getStationIDs());
        return resMap;
    }
    //设备接口状态查询
    @RequestMapping(value ="/query_equip_auth",method = {RequestMethod.POST})
    @ResponseBody
    public TResponse<QueryEquipAuthRes> queryEquipAuth(@RequestBody TResponse<QueryEquipAuthReq> tRequest) throws IOException {
        if (null==tRequest){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        QueryEquipAuthReq queryEquipAuthReq= tRequest.getData();
        if (queryEquipAuthReq==null){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        TResponse<QueryEquipAuthRes> res=queryService.queryEquipAuth(queryEquipAuthReq);
        return res;
    }

    /**
     * 查询业务基本策略结果
     * */
    @RequestMapping(value ="/query_equip_business_policy",method = {RequestMethod.POST})
    @ResponseBody
    public TResponse<QueryEquipBusinessPolicyResForFD> QueryEquipBusinessPolicy(@RequestBody TResponse<QueryEquipBusinessPolicyReqForFD> tRequest) throws IOException {
        if (null==tRequest){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        QueryEquipBusinessPolicyReqForFD queryEquipAuthReq= tRequest.getData();
        if (queryEquipAuthReq==null){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        TResponse<QueryEquipBusinessPolicyResForFD> res=queryService.QueryEquipBusinessPolicy(queryEquipAuthReq);
        return res;
    }
    /**
     *查询充电状态
     * */
    @RequestMapping(value ="/query_equip_charge_status",method = {RequestMethod.POST})
    @ResponseBody
    public TResponse<QueryEquipChargeStatsResForFD> queryEquipChargeStatus(@RequestBody TResponse<QueryEquipChargeStatsReqForFD> tRequest) throws IOException {
        if (null==tRequest){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        QueryEquipChargeStatsReqForFD queryEquipAuthReq= tRequest.getData();
        if (queryEquipAuthReq==null){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }

        TResponse<QueryEquipChargeStatsResForFD> res=queryService.queryEquipChargeStatus(queryEquipAuthReq);
        return res;
    }

    /**
     *查询统计信息
     * */
    @RequestMapping(value ="/query_station_stats",method = {RequestMethod.POST})
    @ResponseBody
    public TResponse<StationStatsInfo> queryStationStats(@RequestBody TResponse<QueryStationStats> tRequest) throws IOException {
        if (null==tRequest){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }
        QueryStationStats queryEquipAuthReq= tRequest.getData();
        if (queryEquipAuthReq==null){
            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
        }

        TResponse<StationStatsInfo> res=queryService.queryStationStats(queryEquipAuthReq);
        return res;
    }

//    /**
//     * 启动充电
//     * */
//    @RequestMapping(value ="/query_start_charge",method = {RequestMethod.POST})
//    @ResponseBody
//    public TResponse<QueryStartChargeResForFD> queryStartCharge(@RequestBody TResponse<QueryStartChargeReqForFD> tRequest) throws IOException {
//        if (null==tRequest){
//            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
//        }
//        QueryStartChargeReqForFD req= tRequest.getData();
//        if (req==null){
//            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
//        }
//        TResponse<QueryStartChargeResForFD> res= pushService.queryStartCharge(req);
//        ObjectMapper objectMapper = new ObjectMapper();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//        String timeStamp = dateFormat.format(System.currentTimeMillis());
//        String data = objectMapper.writeValueAsString(res);
//        String sig = PubFetchTool.genDDSig(data, timeStamp,GDEVConfig.SIGN_KEY);
//        res.setSig(sig);
//        return res;
//    }

    /**
     *结束充电
     * */
//    @RequestMapping(value ="/query_stop_charge",method = {RequestMethod.POST})
//    @ResponseBody
//    public TResponse<QueryStopChargeResForFD> QueryStopCharge(@RequestBody TResponse<QueryStopChargeReqForFD> tRequest) throws IOException {
//        if (null==tRequest){
//            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
//        }
//        QueryStopChargeReqForFD req= tRequest.getData();
//        if (req==null){
//            return PubFetchTool.genErrorResp("4003","POST参数不法，缺少必须的示例");
//        }
//        TResponse<QueryStopChargeResForFD>  res=pushService.QueryStopCharge(req);
//        ObjectMapper objectMapper = new ObjectMapper();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
//        String timeStamp = dateFormat.format(System.currentTimeMillis());
//        String data = objectMapper.writeValueAsString(res);
//        String sig = PubFetchTool.genDDSig(data, timeStamp,GDEVConfig.SIGN_KEY);
//        res.setSig(sig);
//        return res;
//    }


    }
